// ModelViewer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ModelViewer.h"

ModelViewer::ModelViewer()
:m_pEntity(NULL), m_pEntityNode(NULL)
{

}

ModelViewer::~ModelViewer()
{

}

void ModelViewer::createScene()
{	
	m_pSceneMgr->setAmbientLight(ColourValue(1.0f, 1.0f, 1.0f));
	/*
	Entity* pEntity = m_pSceneMgr->createEntity("../../media/models/TMP/tmp.mesh");
	SceneNode* pNode = m_pRootSceneNode->createChildSceneNode("pEntityNode");
	pNode->attachObject(pEntity);
	*/
	//m_pCam->setPosition(0, 0, -30);
	//m_pCam->lookAt(pNode->getPosition());
	
	/*
	
	m_pUIRender = &CEGUI::OgreRenderer::bootstrapSystem();
	using namespace CEGUI;

	// we will use of the WindowManager.
	WindowManager& winMgr = WindowManager::getSingleton();

	// load scheme and set up defaults
	SchemeManager::getSingleton().create("TaharezLook.scheme");
	System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
	CEGUI::FontManager::getSingleton().create("DejaVuSans-10.font");

	CEGUI::Window* myRoot = CEGUI::WindowManager::getSingleton().createWindow( "DefaultWindow", "_MasterRoot" );
	CEGUI::System::getSingleton().setGUISheet( myRoot );

	//WindowManager& winMgr = WindowManager::getSingleton();
	Window* root = winMgr.getWindow("_MasterRoot");

	// create the main list.
	MultiColumnList* mcl = static_cast<MultiColumnList*>(winMgr.createWindow("TaharezLook/MultiColumnList", "Demo6/MainList"));
	root->addChildWindow(mcl);
	mcl->setPosition(UVector2(cegui_reldim(0.01f), cegui_reldim( 0.1f)));
	mcl->setSize(UVector2(cegui_reldim(0.5f), cegui_reldim( 0.8f)));
	*/
	/*// load an image to use as a background
	ImagesetManager::getSingleton().createFromImageFile("BackgroundImage", "GPN-2000-001437.tga");

	// here we will use a StaticImage as the root, then we can use it to place a background image
	Window* background = winMgr.createWindow("TaharezLook/StaticImage", "root_wnd");
	// set position and size
	background->setPosition(UVector2(cegui_reldim(0), cegui_reldim( 0)));
	background->setSize(UVector2(cegui_reldim(1), cegui_reldim( 1)));
	// disable frame and standard background
	background->setProperty("FrameEnabled", "false");
	background->setProperty("BackgroundEnabled", "false");
	// set the background image
	background->setProperty("Image", "set:BackgroundImage image:full_image");
	// install this as the root GUI sheet
	System::getSingleton().setGUISheet(background);*/
}

void ModelViewer::createFrameListener()
{
	m_pFrameListener = new ModelViewFrameListener(mWindow);
	m_pUI = new ModelViewerUI;	
	m_pUI->setModelViewer(this);
	m_pFrameListener->setUIBase(m_pUI);
	m_cameraMgr.setCamera(m_pCam);
	m_pFrameListener->setCameraManager(&m_cameraMgr);
	mRoot->addFrameListener(m_pFrameListener);
}

Ogre::Entity* ModelViewer::loadEntity(const Ogre::String& modelName)
{
	if(m_pEntity)
	{
		printf("destroyEntity\n");
		m_pSceneMgr->destroyEntity(m_pEntity);
	}
	if(NULL == m_pEntityNode)
	{
		printf("new EntityNode\n");
		m_pEntityNode = m_pRootSceneNode->createChildSceneNode();
	}
	printf("new Entity\n");
	m_pEntity = m_pSceneMgr->createEntity(modelName, modelName);
	printf("attach Entity\n");
	m_pEntityNode->attachObject(m_pEntity);
	return m_pEntity;
}

void ModelViewer::playAnimation(const Ogre::String& aniName, bool isLoop /* = false */)
{
	if(aniName == "")
		m_pFrameListener->setAnimationState(NULL);
	else if(m_pEntity)
	{
		Ogre::AnimationState* pState = m_pEntity->getAnimationState(aniName);
		if(pState)
		{
			pState->setEnabled(true);
			pState->setLoop(isLoop);
			pState->setTimePosition(0.0f);
			m_pFrameListener->setAnimationState(pState);
		}
	}
}